package com.example.annolog.service;

import com.example.annolog.domain.City;
import com.example.annolog.mapper.CityMapper;
import com.example.annolog.resp.CityResp;
import com.example.annolog.thread.NormalThread;
import com.example.annolog.thread.ThreadPoolLogic;
import com.example.annolog.utils.ThreadPoolExecutorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author
 */

@Service
public class CityService {
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ThreadPoolLogic threadPoolLogic;

    public List<City> queryList() {

        List<City> list = new ArrayList<>();
        list = cityMapper.selectALLof();
        //一些实现异步的方式
        //方法一
        threadPoolLogic.execute(new NormalThread(new Runnable() {
            @Override
            public void run() {
                City city = new City();
                try {
                    city.setName(String.valueOf(Math.random()));
                    Thread.sleep(10000);
                    System.out.println("------------------" + city.getName() + "-------------");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }));


        //方法二
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("--------do sth--------------");
                }
            }
        }.start();

        //方法三  线程池
        ThreadPoolExecutorUtils.threadPoolExecutor.execute(() -> {
            try {
                System.out.println(new Date() + "-----" + Thread.currentThread().getName() + "正在被执行");
                Thread.sleep(2000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        //longtime();

        return list;
    }

}
